<%@ page contentType="text/html;charset=utf-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html>
	<head>
		<title>公寓列表</title>
		<link rel="stylesheet"
			href="<%=request.getContextPath()%>/css/common.css" type="text/css" />
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/interface/buildingsMgrAction.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/engine.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/dwr/util.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/jquery-latest.pack.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/noConflictOfJquery.js'></script>
		<script type='text/javascript'
			src='<%=request.getContextPath()%>/js/forCSS.js'></script>
		<script type="text/javascript">
	//全局变量
	var apartmtCache = { }; //apartment的copy
	var viewed = -1;
	var eid ;
	var totalcount =-1;
	var page;
	
	function listSearchTotalApartment(){
	buildingsMgrAction.getAprtTotalRows(listapartrowscb);
	}
	 
	var listapartrowscb =  function listApartment(rows){
			totalcount = rows;	 //给总行数赋值
			buildingsMgrAction.ListAllApartmentListByPage(1,totalcount,listapartcallback);	
	}
	var listapartcallback = function listapartcallbackfunc(aptlst){
		if($('aprtinfodiv').style.visibility == 'visible'){
			$('aprtinfodiv').style.visibility = 'hidden';
		}
		if($('pageTable').style.visibility == 'hidden'){
			$('pageTable').style.visibility = 'visible';
		}
		DWRUtil.removeAllRows('ttb', { filter:function(tr) {
		      return (tr.id != "pattern");
			    }});
		var al,id;
		 for (var i =0; i < aptlst.length ; i++) {
			  al = aptlst[i];
			  id = al.apartmentid;
			  dwr.util.cloneNode("pattern", { idSuffix:id });
		     dwr.util.setValue("tableid"+id, al.apartmentid);
		     dwr.util.setValue("tablename"+id, al.apartmentname);
		     dwr.util.setValue("tabletotalrooms"+id, al.apartmenttotalrooms);
		      dwr.util.setValue("tablefloornum"+id,al.apartmentfloornum);
		      dwr.util.setValue("tablemgrname"+id,al.apartmentmgrname);
		      dwr.util.setValue("tablemgrtele"+id,al.apartmenttelephone);
		      dwr.util.setValue("cbx" + id, al.apartmentid);
		 	 $("pattern" + id).style.display= "";
		 	apartmtCache[id] = al;
		   }
		   setPageInfo(); //这里写页面信息
		   tbCSS();
	}
	
	
	function setPageInfo(){
		$('pageTd').innerHTML = '';
		buildingsMgrAction.getPage({
			callback:function(data){
				page = data;
				//显示 当前页/总页数
				if(page.totalPage != 0){
					var pageSpan = document.createElement('span');
					//pageSpan.innerHTML = page.currentPage + '/' + page.totalPage;
					pageSpan.innerHTML = '共 '+totalcount+' 条 '+'   第 '+page.currentPage+' 页    共'+page.totalPage+' 页 ';
					$('pageTd').appendChild(pageSpan);
					var aFirst = document.createElement('a');
					aFirst.setAttribute('href','javascript:getFirstData()');
					aFirst.innerHTML = '首页';
					$('pageTd').appendChild(aFirst);
					var aPrevious = document.createElement('a');
					aPrevious.setAttribute('href','javascript:getPreviousData()');
					aPrevious.innerHTML = '上一页 ';
					$('pageTd').appendChild(aPrevious);
					var aNext = document.createElement('a');
					aNext.setAttribute('href','javascript:getNextData()');
					aNext.innerHTML = '下一页 ';
					$('pageTd').appendChild(aNext);
					var aLast = document.createElement('a');
					aLast.setAttribute('href','javascript:getLastData()');
					aLast.innerHTML = '尾页';
					$('pageTd').appendChild(aLast);
				}
			}
		});
		$('loading').style.display = 'none';
	}

	function getFirstData(){
		page.currentPage = 1;
		getDataPerPage();
	}
	
	function getPreviousData(){
		if(page.currentPage>1){
			page.currentPage--;
		}
		getDataPerPage();
	}
	
	function getNextData(){
		if(page.currentPage<page.totalPage){
			page.currentPage++;
		}
		getDataPerPage();
	}
	
	function getLastData(){
		page.currentPage = page.totalPage;
		getDataPerPage();
	}
	
	function getDataPerPage(){
		$('loading').style.display = 'block';
		buildingsMgrAction.ListAllApartmentListByPage(page.currentPage,totalcount,listapartcallback);	
	}	
	
	function editClicked(eleid) {
		if($('aprtinfodiv').style.visibility == 'hidden'){
			$('aprtinfodiv').style.visibility = 'visible';
		}
		  //得到的eleid为"edit{id}"，如 "edit42".,经过substring后为42,person的id
		  eid = eleid.substring(4);
		  //alert(eid);
		  var aprt = apartmtCache[eid];  
		   //alert(aprt[eid-1].apartmentname)
		   dwr.util.setValues(aprt);   
		}
	
	function delClicked(eleid){
		//得到的eleid为"mydelete{id}"
		var ap_id = eleid.substring(8);
		  //alert(ap_id);
		  buildingsMgrAction.delelteApartmentByID(ap_id,delcb)
		}
	var delcb = function del_cb(stas){
			alert(stas);
			listSearchTotalApartment();
		}
	
	function writeApart() {
		var apt = { apartmentid:null, apartmentname:null, apartmentfloornum:null, apartmentmgrname:null,apartmenttelephone:null,apartmenttotalrooms:null,apartmentavailrooms:null,apartmentind:null };
		dwr.util.getValues(apt);
		var ay = new Array(apt.apartmentid,apt.apartmentname,apt.apartmentfloornum,apt.apartmentmgrname,apt.apartmenttelephone,apt.apartmenttotalrooms,apt.apartmentavailrooms,apt.apartmentind);
		  //alert(apt.apartmentid)
		  //alert(apt.apartmentname=="")
		  if(apt.apartmentname==""){
				alert("请填写公寓名");
				return;
			  }
		  if(apt.apartmentfloornum==""){
				alert("请填写楼层数目");
				return;
			  }
		  //alert(ay+"");
		  buildingsMgrAction.updateApartment(ay+"",writecallback);
		}
		 var writecallback = function wcallback(status){
		 alert(status);
		 getDataPerPage();
		 }
		 
		function clearApart() { //id没有更新,相当于清除充填
		  //alert('clear');
		  dwr.util.setValues({apartmentname:null,apartmentmgrname:null,apartmenttelephone:null });
		}

	
</script>
	</head>
	<body>
		<div id="page_container">
			<%@include file="mgrbanner.jsp"%>
			<h2>
				公寓列表
			</h2>
			<!-- 页面隐藏值开始 -->
			<div id="tableid" style="display: none">
			</div>
			<!-- 页面隐藏值结束 -->
			<table id="MyTable" class="table_common table_list_style">
				<thead>
					<tr>
						<th width="25%">
							<div>
								公寓名称
							</div>
						</th>
						<th width="14%">
							<div>
								楼层总数
							</div>
						</th>
						<th width="14%">
							<div>
								宿舍总数
							</div>
						</th>
						<th width="14%">
							<div>
								管理员
							</div>
						</th>
						<th width="18%">
							<div>
								公寓电话
							</div>
						</th>
						<th width="8%">
							<div>
								编辑
							</div>
						</th>
						<th width="8%">
							<div>
								删除
							</div>
						</th>
					</tr>
				</thead>
				<tbody id="ttb">
					<tr id="pattern" style="display: none;">
						<!-- <td>
							<div>
								<input type="checkbox" id="cbx" name="cbx" />
							</div>
						</td>
						-->
						<td>
							<div id="tablename">
							</div>
						</td>
						<td>
							<div id="tablefloornum">
							</div>
						</td>
						<td>
							<div id="tabletotalrooms">
							</div>
						</td>
						<td>
							<div id="tablemgrname">
							</div>
						</td>
						<td>
							<div id="tablemgrtele">
							</div>
						</td>
						<td>
							<div>
								<a href="#" id="edit" onclick="javascript:editClicked(this.id)"><img
										src="../images/037.gif" width="9" height="9" />
								</a>
							</div>
						</td>
						<td>
							<div>
								<a href="#" id="mydelelt"
									onclick="javascript:if(confirm('确认删除！')) delClicked(this.id)">
									<img src="../images/010.gif" width="9" height="9" />
								</a>
							</div>
						</td>
					</tr>
				</tbody>
			</table>
			<!-- 分页div -->
			<table id="pageTable" align="center"
				style="visibility: hidden; margin-top: 30px; font: 12px;"
				class="navigator">
				<tr>
					<td id="pageTd"></td>
				</tr>
			</table>
			<div id="loading"
				style="display: none; margin-top: 20px; text-align: center; color: red; font: 13px;">
				Loading...
			</div>
			<!-- 分页div结束 -->

			<div id="aprtinfodiv" style="visibility: hidden;">
				<table>
					<tr>
						<td>
							公寓名:
						</td>
						<td>
							<input id="apartmentname" type="text" />
							<input id="apartmentid" type="hidden" />
							<input id="apartmentind" type="hidden" />
							<input id="apartmentavailrooms" type="hidden" />
						</td>
					</tr>
					<tr>
						<td>
							楼层数：
						</td>
						<td>
							<input id="apartmentfloornum" type="text" readonly />
						</td>
					</tr>
					<tr>
						<td>
							管理员：
						</td>
						<td>
							<input type="text" id="apartmentmgrname" />
						</td>
					</tr>
					<tr>
						<td>
							公寓电话：
						</td>
						<td>
							<input type="text" id="apartmenttelephone" />
						</td>
					</tr>
					<tr>
						<td>
							总房间数：
						</td>
						<td>
							<input type="text" id="apartmenttotalrooms" readonly />
						</td>
					</tr>
					<tr>
						<td colspan="2" align="right">
							<input class="common_button" type="button" value="保存" onclick="writeApart()" />
							<input class="common_button" type="button" value="清除" onclick="clearApart()" />
						</td>
					</tr>
				</table>
			</div>
		</div>
		<script type="text/javascript">	
			window.onload = function(){
				var nav_a=$j('#page_nav a');
				var nav_span=$j("#page_nav a span");
				for(var u = 0 ;u<nav_a.length;u++){
						nav_a.get(u).style.backgroundPosition="left top";
						nav_span.get(u).style.backgroundPosition="right top";							
				}
				$j('#page_nav a').get(0).style.backgroundPosition="0% -42px";
				$j("#page_nav a span").get(0).style.backgroundPosition="100% -42px";
				listSearchTotalApartment();
			}	
		</script>
	</body>
</html>
